<div class="modal-dialog modal-dialog-scrollable modal-dialog-centered ">
	<div class="modal-content">
					<div class="modal-header">
						<h6 class="modal-title">期初调整</h6>
						<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
					</div>
					<div class="modal-body">
					<div class="alert alert-warning" role="alert">
						  <i class="bi bi-shield-exclamation"></i>期初调整：调整客户在系统上线使用之前剩余未收的欠款金额，调整后，基本不会发生变更，此功能权限建议不要随便开放
					</div>
					<form id="customerBill-editOpenBalance-form" onsubmit="customerBill.updateOpenBalance();return false;">
					<div class="row">
					<div class="col-1"></div>
					<div class="col">
						<div class="input-group mb-3">
							<span class="input-group-text">客户<span class="text-danger">*</span></span> 
							<input type="hidden" name="customer_info_id" id="customerBill_customer_info_id">
							<input type="text"  placeholder="选择客户" name="customer_info_keyword" id="customerBill_customer_info_keyword" autocomplete="off" class="form-control">
							
						</div>
					</div>
					<div class="col-1"></div>
				</div>
				<div class="row">
					<div class="col-1"></div>
					<div class="col">
						<div class="input-group mb-3">
							<span class="input-group-text">期初欠款<span class="text-danger">*</span><i class="bi bi-question-circle" data-bs-toggle="tooltip" data-bs-placement="top" title="“欠”表示客户还欠您的货款，“余”表示客户在您这里还有预存余额"></i></span> 
							<select class="form-select" name="amount_type" id="customerBill_amount_type" style="max-width:5rem">
								<option value="1">欠</option>
								<option value="-1">余</option>
							</select>
							<input type="text" maxlength="20" class="form-control" name="open_balance" id="customerBill_open_balance" placeholder="请输入客户的期初欠款" onclick="$(this).select();">
						</div>
					</div>
					<div class="col-1"></div>
				</div>
				<div class="row">
					<div class="col-1"></div>
					<div class="col text-secondary" id="customerBill_open_balance_logs">
						
					</div>
					<div class="col-1"></div>
				</div>
					</form>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-secondary" data-bs-dismiss="modal" aria-label="Close">关闭</button>
						<button type="button" class="btn btn-primary" onclick="customerBill.updateOpenBalance();">保存</button>
					</div>
				</div>
</div>

<script type="application/javascript">

$(function() {
	// 初始化bootstrap相关组件
	common.initBootstrap();
	customerBill.customerList();
	formInput.listenkeyUp("#customerBill-editOpenBalance-form");
	
	setTimeout(function() {
		$("#customerBill_customer_info_id").focus();
	}, 200);
	
});

customerBill.updateOpenBalance = function() {
	if($.trim($("#customerBill_customer_info_id").val()) == '') {
		beekit.tips('客户不能为空！', '#customerBill_customer_info_keyword');
		$("#customerBill_customer_info_keyword").focus();
		return false;
	}
	if($.trim($("#customerBill_open_balance").val()) == '') {
		beekit.tips('期初欠款不能为空！', '#customerBill_open_balance');
		$("#customerBill_open_balance").focus();
		return false;
	}
	
	beekit.ajaxPost("/fund/book/customerBill/updateOpenBalance", $("#customerBill-editOpenBalance-form").serialize(), function(ret) {
		common.processRequestResult(ret, function(ret) {
			beekit.msg(ret);
			if (ret.state == "ok") {
				beekit.modalClose();
				customerBill.list(ret.targetId);
			}
		});
	});
};

customerBill.customerList = function() {
	beekit.autocomplete("#customerBill_customer_info_keyword", "/sale/customer/info/listByJson", "#customerBill_customer_info_id", function(dataSelected) {
		beekit.ajaxPost("/fund/book/customerBill/openBalanceLog", {"customer_info_id":dataSelected.id}, function(ret) {
			if (ret.state == "ok") {
				var openBalance = ret.openBalance;
				if(openBalance > 0) {
					$("#customerBill_amount_type").val(1);
					$("#customerBill_open_balance").val(openBalance);
				} else if(openBalance < 0){
					$("#customerBill_amount_type").val(-1);
					$("#customerBill_open_balance").val(0-openBalance);
				} else {
					$("#customerBill_open_balance").val(0);
				}
				$("#customerBill_open_balance").select();
				var logs = $("#customerBill_open_balance_logs");
				logs.html("");
				$.each(ret.logs,function(index,log) {
					var amount = "";
					if(log.amount > 0) {
						amount = "欠"+log.amount;
					} else {
						amount = "余"+(0-log.amount);
					}
					logs.append("●&nbsp;"+log.created_at+"&nbsp;&nbsp;&nbsp;&nbsp;"+log.remark+"&nbsp;&nbsp;&nbsp;&nbsp;"+amount);
					logs.append("<hr class=\"row-hr\" />");
				});
			}
		});
	});
};

</script>
